package org.juggers.servlet;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.ArrayList;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.juggers.component.Captcha;
import org.juggers.domain.AlreadyConfirmedException;
import org.juggers.domain.AlreadyRegisteredException;
import org.juggers.domain.InvalidTokenException;
import org.juggers.domain.Jugger;
import org.juggers.domain.SendMailException;
import org.juggers.services.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author ndeloof
 */
@Controller
public class RegistrationController
{
    @Autowired
    private RegistrationService registrationService;

    @Autowired
    private Captcha captcha;

    @RequestMapping( value = "/registration/register.do", method = RequestMethod.POST )
    public ModelAndView register( @RequestParam( "eMail" ) String eMail, @RequestParam( "firstName" ) String firstName,
                                  @RequestParam( "lastName" ) String lastName, ModelMap model )
        throws AlreadyRegisteredException, SendMailException
    {
        Jugger jugger = new Jugger();
        jugger.setEMail( eMail );
        jugger.setFirstName( firstName );
        jugger.setLastName( lastName );

        registrationService.register( jugger );

        model.addAttribute( "jugger", jugger );
        return new ModelAndView( "registered", model );
    }

    @Transactional
    @RequestMapping( value = "/registration/list.do", method = RequestMethod.GET )
    public ModelAndView list( ModelMap model )
    {
        model.addAttribute( "juggers", new ArrayList<Jugger>( Jugger.findAll() ) );
        return new ModelAndView( "list", model );
    }

    @RequestMapping( value = "/registration/confirm.do", method = RequestMethod.GET )
    public ModelAndView confirm( @RequestParam( "eMail" ) String eMail, @RequestParam( "token" ) String token,
                                 ModelMap model )
        throws AlreadyConfirmedException, InvalidTokenException
    {
        registrationService.confirm( eMail, token );

        model.addAttribute( "email", eMail );
        return new ModelAndView( "confirmed", model );
    }

    @RequestMapping( value = "/registration/captcha.do", method = RequestMethod.GET )
    public ModelAndView showCaptcha( ModelMap model )
    {
        model.addAttribute( "captcha", captcha.getHtml() );
        return new ModelAndView( "captcha", model );
    }

    @RequestMapping( value = "/registration/captcha.do", method = RequestMethod.POST )
    public ModelAndView validCaptcha( HttpServletRequest request,
                                      @RequestParam( "recaptcha_challenge_field" ) String challenge,
                                      @RequestParam( "recaptcha_response_field" ) String response, ModelMap model )
    {
        if ( captcha.isValid( challenge, response, request.getRemoteAddr() ) )
        {
            return new ModelAndView( "validCaptcha" );
        }

        model.addAttribute( "captcha", captcha.getHtml() );
        return new ModelAndView( "captcha", model );
    }

    @Transactional
    @RequestMapping( value = "/registration/setadmin.do", method = RequestMethod.GET )
    public ModelAndView setAdmin( @RequestParam( "email" ) String email )
    {
        Jugger jugger = Jugger.findByEMail( email );
        jugger.getRoles().add( "ROLE_JUG_ADMIN" );
        
        return new ModelAndView( "confirmed" );
    }

}
